package gui.media;

import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Date: Aug 14, 2008
 * Time: 12:35:34 AM
 * To change this template use File | Settings | File Templates.
 *
 * @author Jose Martin Rozanec;
 */
public class PanelMediaThread extends Thread {
    private URL mediaURL;

    public PanelMediaThread(URL mediaURL) {
        this.mediaURL = mediaURL;
    }

    public void run() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        Player mediaPlayer = videoExecution(panel);
        frame.setContentPane(panel);
        frame.setSize(500, 500);
        frame.setVisible(true);
//            mediaURL.start();
        long vuelta = 0;
        boolean yaFue500 = false;
        while (true) {
            if (mediaPlayer.getState() == 500 && !yaFue500) {
                yaFue500 = true;
//                frame=new JFrame();
                mediaPlayer = videoExecution(panel);
            }
            if (mediaPlayer.getState() != 500) {
                yaFue500 = false;
            }
            System.out.println("" + (++vuelta));
            System.out.println("" + mediaPlayer.getState());

            try {
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private Player videoExecution(JPanel panel1) {
        panel1.removeAll();
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
        try {
            // create a mediaPlayer to play the media specified in the URL
            Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);
            // get the components for the video and the playback controls
            Component video = mediaPlayer.getVisualComponent();
//         Component controls = mediaPlayer.getControlPanelComponent();

            if (video != null)
                panel.add(video, BorderLayout.CENTER); // add video component

//         if ( controls != null )//todo agrega controles de audio, video y play/pausa
//            add( controls, BorderLayout.SOUTH ); // add controls
            panel.revalidate();
            panel1.add(panel);
            panel1.revalidate();
            mediaPlayer.start(); // start playing the media clip
            return mediaPlayer;

        } // end try
        catch (NoPlayerException noPlayerException) {
            System.err.println("No media mediaPlayer found");
        } // end catch
        catch (CannotRealizeException cannotRealizeException) {
            System.err.println("Could not realize media mediaPlayer");
        } // end catch
        catch (IOException iOException) {
            System.err.println("Error reading from the source");
        }
        return null;
    }
}
